<?php

namespace App\Admin\Controllers;

use App\Models\Article;
use App\Models\Journal;
use App\Models\Issue;
use App\Models\ArticleCat;
use App\Models\NewsCat;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ArticleController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        return Admin::grid(Article::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('ID')->sortable();
            $grid->title("标题");
            $grid->view_count("浏览量");
            $grid->download_count("下载量");
            $grid->ArticleCat()->name('类别');
            $states = [
                'on' => ['value' => 1, 'text' =>'Yes'],
                'off' => ['value' => 0, 'text' => 'No'],
            ];

            $grid->column('选项卡')->switchGroup([
                'is_featured' => '精选文章', 'is_pic' => '滚动图片'
            ], $states);

            $grid->accept_date("接收时间")->display(function($date) {
                return date('Y-m-d', strtotime($date));
            });
            
            $grid->publish_date("发表时间")->display(function($date) {
                return date('Y-m-d', strtotime($date));
            });

            $grid->column("发表周期")->display(function() {
                $diff = function ($date_1 , $date_2 , $differenceFormat = '%a' )
                {
                    $datetime1 = date_create($date_1);
                    $datetime2 = date_create($date_2);
                    
                    $interval = date_diff($datetime1, $datetime2);
                    
                    return $interval->format($differenceFormat);
                    
                };

                return $diff($this->accept_date, $this->publish_date, '%a 天');
            });

            $grid->filter(function($filter){
                $filter->like('title');
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                
                $actions->disableView();
            });
            // $grid->tools(function (Grid\Tools $tools) {
            //     $tools->batch(function (Grid\Tools\BatchActions $actions) {
            //         $actions->disableDelete();
            //     });
            // });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Article::class, function (Form $form) {
            // $form->display('id', 'ID')->default('ZR-{id}');
            $form->display('id', 'ID');
            $form->
                tab('Article Info', function($form) {
                    $journals = Journal::all();
                    $journalsList = [];
                    foreach ($journals as $j) {
                        $journalsList[$j->id] = $j['name_short'];
                    }
                    $form->select('journal_id', '期刊')->options($journalsList);
                    $issues = Issue::all();
                    $issue = [];
                    foreach ($issues as $key => $value) {
                        $issue[$value->id] = $value['year'] .'-'. $value['volume'] .'-'. $value['issue'];
                    }
                    // $issue_value = max($issue);
                    // $issue_key = array_keys($issue, max($issue));
                    // $issueList = [];
                    // foreach ($issue_key as $key => $value) {
                    //     $issueList[$value] = $issue_value;
                    // }
                    $form->select('issue_id', '年卷期')->options($issue);
                    $form->text('page_start', '起始页')->rules('required|regex:/^\d+$/', ['regex'=>'填写正确页数']);
                    $form->text('page_end', '结束页');
                    $catesArr = [];
                    $cates = ArticleCat::all();
                    foreach ($cates as $cate) {
                        $catesArr[$cate->id] = $cate->name;
                    }
                    $form->select('cate_id', '栏目')->options($catesArr)->default(7); // 7 articles

                    $form->text('title', '标题');
                    $form->text('id_doi', 'Doi');
                    $form->editor('abstract', '摘要');
                    $form->text('keywords', '关键词');

                    $form->editor('author_list', '作者');
                    $form->editor('affiliation', '单位');

                })->
                tab('Other', function($form) {
                    $form->number('download_count', '下载量');
                    $form->number('view_count', '浏览量');
                    $form->number('cite_count', 'WOS引用量');
                    $form->editor('fund', '基金信息');
                    $form->multipleImage('imgs', '图片')->removable();
                    $form->file('pdf', 'PDF');
                    $form->file('xml', 'XML');
                    $form->text('html', 'HTML 路径');

                    $states = [
                        'on' => ['value' => 1, 'text' => 'Yes'],
                        'off' => ['value' => 0, 'text' =>'No'],
                    ];

                    $form->switch('is_pic', '滚动图片')->states($states);
                    $form->switch('is_featured', '精选文章')->states($states);

                    $form->date('accept_date', '接收时间')->format('YYYY-MM-DD');
                    $form->date('revision_date', '修订时间')->format('YYYY-MM-DD');
                    $form->date('publish_date', '发表时间')->format('YYYY-MM-DD');
                });


            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');

            $form->disableViewCheck();
            $form->disableEditingCheck();
            $form->tools(function(\Encore\Admin\Form\Tools $tools) {
                $tools->disableView();
            });
        });
    }

    /**
     * Help message for icon field.
     *
     * @return string
     */
    protected function iconHelp()
    {
        return 'Please use number of affiliations like 1';
    }
}
